package org.jeecg.modules.wlhy.report.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.index.qual.SameLen;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.enums.ReportPlatformEnum;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.CheckBankCardIUtil;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.LngLonUtil;
import org.jeecg.modules.paycenter.channel.bankdocking.utils.BankUtils;
import org.jeecg.modules.third.amap.service.AmapService;
import org.jeecg.modules.third.digitalseal.utils.PdfToImageUtil;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.PaymentMeansCodeEnum;
import org.jeecg.modules.third.jg.report.freight.information.dto.DriverDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.VehicleDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.enterprise.AppsDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.enterprise.EnterpriseDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.financial.FinancialDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.financial.FinanciallistDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.financial.ShippingNoteListDTO;
import org.jeecg.modules.third.jg.report.freight.information.dto.shippingnote.*;
import org.jeecg.modules.third.jg.xz.park.XzIndustrialParkService;
import org.jeecg.modules.third.jg.xz.park.dto.payment.XzFinancialListDTO;
import org.jeecg.modules.third.jg.xz.park.dto.payment.XzPaymentDTO;
import org.jeecg.modules.third.jg.xz.park.dto.payment.XzShippingNoteListDTO;
import org.jeecg.modules.third.jg.xz.park.dto.shippingnote.*;
import org.jeecg.modules.third.openapi.WaybillRouteService;
import org.jeecg.modules.third.openapi.dto.report.capital.CapitalDTO;
import org.jeecg.modules.third.openapi.dto.report.capital.ZjFinanciallist;
import org.jeecg.modules.third.openapi.dto.report.capital.ZjShippingNoteList;
import org.jeecg.modules.third.openapi.dto.report.waybill.*;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.company.entity.HyCompany;
import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
import org.jeecg.modules.wlhy.company.service.IHyCompanyConfigService;
import org.jeecg.modules.wlhy.company.service.IHyCompanyService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressReceive;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressSend;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerCompany;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressReceiveService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressSendService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerCompanyService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.enterprise.entity.HyEnterprise;
import org.jeecg.modules.wlhy.enterprise.entity.HyEnterpriseApps;
import org.jeecg.modules.wlhy.enterprise.service.impl.HyEnterpriseAppsServiceImpl;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicleType;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.entity.WaybillRouteVO;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.BankTypeUtil;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BaseReportService {

    @Resource
    private HyEnterpriseAppsServiceImpl enterpriseAppsService;

    @Resource
    private WaybillRouteService waybillRouteService;

    @Resource
    private IHyCustomerCompanyService hyCustomerCompanyService;

    @Resource
    private IHyVehicleService vehicleService;

    @Resource
    private IHyOrderService orderService;

    @Resource
    private IHyCustomerService customerService;

    @Resource
    private IHyDriverService driverService;

    @Resource
    private IHyShippingNoteService hyShippingNoteService;

    @Resource
    private IHyCustomerAddressSendService customerAddressSendService;

    @Resource
    private IHyCustomerAddressReceiveService customerAddressReceiveService;

    @Resource
    private AmapService amapService;

    /**
     * 申报企业信息
     */
    @Autowired
    private IHyCompanyService companyService;

    @Autowired
    private IHyCompanyConfigService hyCompanyConfigService;

    @Resource
    private XzIndustrialParkService xzIndustrialParkService;

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    @Value(value = "${jeecg.path.bank}")
    private String bankpath;


    /**
     * 组装电子运单报文
     *
     * @param hyShippingNote
     * @return
     */
    public ShippingNoteDTO shippingNoteTOShippingNoteDTO(HyShippingNote hyShippingNote, String companyId) {
        if (hyShippingNote == null) {
            throw new JeecgBootException("该运单上报信息不能为空");
        }

        HyCustomer hyCustomer = customerService.getById(hyShippingNote.getCustomerId());
        if (hyCustomer == null) {
            throw new JeecgBootException("该运单货主信息不能为空");
        }

        HyOrder hyOrder = orderService.getById(hyShippingNote.getOrderId());
        if (hyOrder == null) {
            throw new JeecgBootException("该运单货源信息不能为空");
        }

        HyDriver hyDriver = driverService.getById(hyShippingNote.getDriverId());

        if (hyDriver == null) {
            throw new JeecgBootException("该运单司机信息不能为空");
        }
        HyVehicle hyVehicle = vehicleService.getById(hyShippingNote.getVehicleId());
        if (hyVehicle == null) {
            throw new JeecgBootException("该运单车辆信息不能为空");
        }

        HyCompany company = companyService.getById(companyId);
        if (company == null) {
            throw new JeecgBootException("该运单公司信息不能为空");
        }

        HyCompanyConfig companyConfig = hyCompanyConfigService.getByCompanyId(company.getId());
        if (companyConfig == null) {
            throw new JeecgBootException("该运单公司配置信息不存在");
        }

        ShippingNoteDTO shippingNoteDTO = new ShippingNoteDTO();

        shippingNoteDTO.setOriginalDocumentNumber(hyOrder.getOrderNum());
        String shippingNoteNumber = hyShippingNote.getShippingNoteNumber().replaceAll("-", "");
        if (shippingNoteNumber.length() > 20) {
            shippingNoteNumber = shippingNoteNumber.substring(0, 20);
        }
        shippingNoteDTO.setShippingNoteNumber(shippingNoteNumber);

        //Integer vehicleAmounthy = getVehicleAmounthyByOrderId(hyShippingNote.getOrderId()) ;
        shippingNoteDTO.setVehicleAmount(1);
        //分段分单号 分段运输和多车运输由 四位数字组成， 前两位代表一 单多车的序号，后两位代表分 段序号。
        //若运输形式为一单一 车填 0000。
        shippingNoteDTO.setSerialNumber("0000");
        //运输组织类型代码
        shippingNoteDTO.setTransportTypeCode(1);
        shippingNoteDTO.setSendToProDateTime(DateUtils.dateToStringDateTime(new Date()));

        shippingNoteDTO.setCarrier(company.getCompanyName()
                //                "卡车人网络科技（河北）有限公司"
        );
        shippingNoteDTO.setUnifiedSocialCreditIdentifier(company.getCreditCode()
                //                "91130125MA0F4RRP2C"
        );
        //车辆 道路运输经营许可 网络货运经营者的道路 运输经营许可证编号。
        shippingNoteDTO.setPermitNumber(company.getPermitNumber()
                //                "130125004848"
        );

        shippingNoteDTO.setConsignmentDateTime(DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtReceive()));
        shippingNoteDTO.setBusinessTypeCode(hyShippingNote.getBusinessTypeCode());

        shippingNoteDTO.setDespatchActualDateTime(DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtLoading()));
        shippingNoteDTO.setGoodsReceiptDateTime(DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtUnloading()));

        if (!companyConfig.getReportPlatformType().equals(ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode())) {
            //正常监管上报
            BigDecimal costPay = hyShippingNote.getGoodsWeightLoading().multiply(hyShippingNote.getShippingUnitPrice());
            BigDecimal driverFee = hyShippingNote.getDriverFee() != null ?  hyShippingNote.getDriverFee() : costPay ;
            shippingNoteDTO.setTotalMonetaryAmount(driverFee.setScale(3, BigDecimal.ROUND_DOWN).toString());
        } else {
            //铭纳监管上报2个字段，货主运费 + 实付实际运费
            BigDecimal costPay = hyShippingNote.getGoodsWeightLoading().multiply(hyShippingNote.getShippingUnitPrice());
            BigDecimal customerFee = hyShippingNote.getCustomerFee() != null ?  hyShippingNote.getCustomerFee() : costPay ;
            BigDecimal driverFee = hyShippingNote.getDriverFee() != null ?  hyShippingNote.getDriverFee() : costPay ;
            shippingNoteDTO.setTotalMonetaryAmount(customerFee.setScale(3, BigDecimal.ROUND_DOWN).toString());
            shippingNoteDTO.setTotalPaymentAmount(driverFee.setScale(3, BigDecimal.ROUND_DOWN).toString());
        }

        //托运人信息
        getConsignorInfo(companyConfig, hyCustomer, hyShippingNote, shippingNoteDTO);
        //收货方信息
        getConsigneeInfo(companyConfig, hyShippingNote, shippingNoteDTO);
        //车辆信息
        getVehicleInfo(companyConfig, hyShippingNote, hyDriver, hyVehicle, shippingNoteDTO);
        //实际承运人信息
        getActualCarrierInfo(companyConfig, hyVehicle, hyDriver, shippingNoteDTO);
        //保险信息
        getInsuranceInformation(shippingNoteDTO);

        return shippingNoteDTO;
    }

    /**
     * 保险信息
     *
     * @param shippingNoteDTO
     */
    public void getInsuranceInformation(ShippingNoteDTO shippingNoteDTO) {
        InsuranceInformation insuranceInformation = new InsuranceInformation();
        insuranceInformation.setInsuranceCompanyCode("none");
        insuranceInformation.setPolicyNumber("none");
        //for test
        //insuranceInformation.setInsuranceCompanyCode("YGBX");
        //insuranceInformation.setPolicyNumber("BXDH870958059");

        shippingNoteDTO.setInsuranceInformation(insuranceInformation);
    }
    private void getXzInsuranceInformation(XzShippingNoteDTO shippingNoteDTO) {
        InsuranceInformations insuranceInformations = new InsuranceInformations();
        insuranceInformations.setInsuranceCompanyCode("none");
        insuranceInformations.setPolicyNumber("none");
        shippingNoteDTO.setInsuranceInformation(insuranceInformations);
    }

    private void getZjInsuranceInformation(WaybillDTO shippingNoteDTO) {
        ZjInsuranceInformation insuranceInformations = new ZjInsuranceInformation();
        insuranceInformations.setInsuranceCompanyCode("none");
        insuranceInformations.setPolicyNumber("none");
        shippingNoteDTO.setInsuranceInformation(
                Collections.singletonList(  insuranceInformations)
        );
    }

    /**
     * 实际承运人信息
     *
     * @param hyVehicle
     * @param hyDriver
     * @param shippingNoteDTO
     */
    public void getActualCarrierInfo(HyCompanyConfig companyConfig, HyVehicle hyVehicle, HyDriver hyDriver, ShippingNoteDTO shippingNoteDTO) {

        ActualCarrierInfo actualCarrierInfo = new ActualCarrierInfo();
        //个人实际承运人
        if (hyVehicle.getOwner().contains("公司")) {
            actualCarrierInfo.setActualCarrierName(hyVehicle.getOwner() + "(" + hyDriver.getDriverName() + ")");
        } else {
            actualCarrierInfo.setActualCarrierName(hyDriver.getDriverName());
        }

        //如果业户的经营许可证为空或小于12位，可暂时用道路运输证前6位+000000代替
        if (StrUtil.isEmpty(hyVehicle.getBusinessLicense()) || hyVehicle.getBusinessLicense().length() < 12) {
            if (StrUtil.isEmpty(hyVehicle.getRoadTransportNumber()) || hyVehicle.getRoadTransportNumber().length() <= 6) {
                throw new JeecgBootException("承运车辆的道路运输证号错误");
            }
            actualCarrierInfo.setActualCarrierBusinessLicense(hyVehicle.getRoadTransportNumber().substring(0, 6) + "000000");
        } else {
            actualCarrierInfo.setActualCarrierBusinessLicense(hyVehicle.getBusinessLicense());
        }

//        actualCarrierInfo.setActualCarrierBusinessLicense(
//                StringUtils.isNotBlank(hyVehicle.getBusinessLicense()) ? hyVehicle.getBusinessLicense() :
//                        hyVehicle.getRoadTransportNumber().substring(0, 6) + "000000"
//        );

        if (!companyConfig.getReportPlatformType().equals(ReportPlatformEnum.REPORT_PLATFORM_SHAANXI.getCode())) {
            actualCarrierInfo.setActualCarrierId(hyDriver.getIdcard());
        }
        actualCarrierInfo.setActualCarrierID(hyDriver.getIdcard());

        //  For test
//        actualCarrierInfo.setActualCarrierName("小白");
//        actualCarrierInfo.setActualCarrierBusinessLicense("350628989192");
//        actualCarrierInfo.setActualCarrierId("115396531346589079");
//        actualCarrierInfo.setActualCarrierID("115396531346589079");

        shippingNoteDTO.setActualCarrierInfo(actualCarrierInfo);
    }
    private void getZjActualCarrierInfo(HyVehicle hyVehicle, HyDriver hyDriver, WaybillDTO shippingNoteDTO) {
        ZjActualCarrierInfo actualCarrierInfo = new ZjActualCarrierInfo();
        //个人实际承运人
        if (hyVehicle.getOwner().contains("公司")) {
            actualCarrierInfo.setActualCarrierName(hyVehicle.getOwner() + "(" + hyDriver.getDriverName() + ")");
        } else {
            actualCarrierInfo.setActualCarrierName(hyDriver.getDriverName());
        }
        actualCarrierInfo.setActualCarrierBusinessLicense(
                StringUtils.isNotBlank(hyVehicle.getBusinessLicense()) ? hyVehicle.getBusinessLicense() :
                        hyVehicle.getRoadTransportNumber().substring(0 , 6) +"000000"
                //hyVehicle.getRoadTransportNumber()
        );

        actualCarrierInfo.setActualCarrierID(hyDriver.getIdcard());
        shippingNoteDTO.setActualCarrierInfo(
                Collections.singletonList(actualCarrierInfo)
        );
    }


    private void getXzActualCarrierInfo(HyVehicle hyVehicle, HyDriver hyDriver, XzShippingNoteDTO shippingNoteDTO) {
        ActualCarrierInfos actualCarrierInfos = new ActualCarrierInfos();
        actualCarrierInfos.setActualCarrierName(hyDriver.getDriverName());
        actualCarrierInfos.setActualCarrierBusinessLicense(
                StringUtils.isNotBlank(hyVehicle.getBusinessLicense()) ? hyVehicle.getBusinessLicense() :
                        hyVehicle.getRoadTransportNumber().substring(0 , 6) +"000000"
                //hyVehicle.getRoadTransportNumber()
        );
        actualCarrierInfos.setActualCarrierId(hyDriver.getIdcard());
        shippingNoteDTO.setActualCarrierInfo(actualCarrierInfos);
    }



    /**
     * 车辆信息
     *
     * @param hyShippingNote
     * @param hyDriver
     * @param hyVehicle
     * @param shippingNoteDTO
     */
    private void getVehicleInfo(HyCompanyConfig companyConfig, HyShippingNote hyShippingNote, HyDriver hyDriver, HyVehicle hyVehicle,
                                ShippingNoteDTO shippingNoteDTO) {
        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setVehiclePlateColorCode(hyVehicle.getVehiclePlateColorCode());
        vehicleInfo.setVehicleNumber(hyVehicle.getVehicleNumber());

        //陕西不支持挂车信息上报，如果是陕西客户跳过挂车信息
        if (!companyConfig.getReportPlatformType().equals(ReportPlatformEnum.REPORT_PLATFORM_SHAANXI.getCode())
         && !companyConfig.getReportPlatformType().equals(ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode())) {
            if(StringUtils.isNotEmpty(hyVehicle.getTrailerVehiclePlateNumber())){
                vehicleInfo.setTrailerVehiclePlateNumber(hyVehicle.getTrailerVehiclePlateNumber());
                vehicleInfo.setTrailerVehiclePlateColorCode(hyVehicle.getVehiclePlateColorCode());
            }
        }

        if (!companyConfig.getReportPlatformType().equals(ReportPlatformEnum.REPORT_PLATFORM_MINGNA.getCode())) {
            vehicleInfo.setDespatchActualDateTime(
                    DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtLoading())
            );
            vehicleInfo.setGoodsReceiptDateTime(
                    DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtUnloading())
            );
            vehicleInfo.setPlaceOfLoading(hyShippingNote.getSendAddress());
            vehicleInfo.setGoodsReceiptPlace(hyShippingNote.getReceiveAddress());
            vehicleInfo.setLoadingCountrySubdivisionCode(hyShippingNote.getSendAreaCode());
            vehicleInfo.setReceiptCountrySubdivisionCode(hyShippingNote.getReceiveAreaCode());
        }
        //司机信息
        if (StrUtil.isNotEmpty(hyShippingNote.getActualDriverId())) {
            HyDriver hyActualDriver = this.driverService.getDriverById(hyShippingNote.getActualDriverId());
            if (null != hyActualDriver) {
                getDrivers(hyActualDriver, vehicleInfo);
            } else {
                getDrivers(hyDriver, vehicleInfo);
            }
        } else {
            getDrivers(hyDriver, vehicleInfo);
        }

        //货物信息
        getGoodsInfos(hyShippingNote, vehicleInfo);
        shippingNoteDTO.setVehicleInfo(vehicleInfo);
    }

    private void getZjVehicleInfo(HyShippingNote hyShippingNote, HyDriver hyDriver, HyVehicle hyVehicle,
                                  WaybillDTO shippingNoteDTO) {

        ZjVehicleInfo vehicleInfo = new ZjVehicleInfo();
        vehicleInfo.setVehiclePlateColorCode(hyVehicle.getVehiclePlateColorCode());
        vehicleInfo.setVehicleNumber(hyVehicle.getVehicleNumber());
        vehicleInfo.setDespatchActualDateTime(
                DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtLoading())
        );
        vehicleInfo.setGoodsReceiptDateTime(
                DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtUnloading())
        );
        vehicleInfo.setPlaceOfLoading(hyShippingNote.getSendAddress());
        vehicleInfo.setGoodsReceiptPlace(hyShippingNote.getReceiveAddress());
        vehicleInfo.setLoadingCountrySubdivisionCode(hyShippingNote.getSendAreaCode());
        vehicleInfo.setReceiptCountrySubdivisionCode(hyShippingNote.getReceiveAreaCode());
        //司机信息
        getZjDrivers(hyDriver, vehicleInfo);
        //货物信息
        getZjGoodsInfo(hyShippingNote, vehicleInfo);

        shippingNoteDTO.setVehicleInfo(
                Collections.singletonList( vehicleInfo)
        );
    }

    private void getXzVehicleInfo(HyShippingNote hyShippingNote, HyDriver hyDriver, HyVehicle hyVehicle,
                                  XzShippingNoteDTO shippingNoteDTO) {
        VehicleInfoQuery vehicleInfo = new VehicleInfoQuery();
        vehicleInfo.setVehiclePlateColorCode(Integer.parseInt(hyVehicle.getVehiclePlateColorCode()));
        vehicleInfo.setVehicleNumber(hyVehicle.getVehicleNumber());

        vehicleInfo.setDespatchActualDateTime(
                DateUtils.dateTimeToString(hyShippingNote.getStatusDtLoading())
        );
        vehicleInfo.setGoodsReceiptDateTime(
                DateUtils.dateTimeToString(hyShippingNote.getStatusDtUnloading())
        );
        vehicleInfo.setPlaceOfLoading(hyShippingNote.getSendAddress());
        vehicleInfo.setGoodsReceiptPlace(hyShippingNote.getReceiveAddress());
        vehicleInfo.setLoadingCountrySubdivisionCode(hyShippingNote.getSendAreaCode());
        vehicleInfo.setReceiptCountrySubdivisionCode(hyShippingNote.getReceiveAreaCode());

        vehicleInfo.setDriverLoadDateTime(
                DateUtils.dateTimeToString(hyShippingNote.getStatusDtLoading())
        );

        if (ObjectUtil.isEmpty(hyShippingNote.getSendLongitude()) || ObjectUtil.isEmpty(hyShippingNote.getSendLatitude())) {
            String sendCoordinate = this.amapService.coordinate(hyShippingNote.getSendAddress());
            vehicleInfo.setDriverLoadLonlat(sendCoordinate);
        } else {
            vehicleInfo.setDriverLoadLonlat(
                    hyShippingNote.getSendLongitude().toString()+","+hyShippingNote.getSendLatitude().toString()
            );
        }

        vehicleInfo.setDriverUnloadDateTime(
                DateUtils.dateTimeToString(hyShippingNote.getStatusDtUnloading())
        );

        if (ObjectUtil.isEmpty(hyShippingNote.getReceiveLongitude()) || ObjectUtil.isEmpty(hyShippingNote.getReceiveLatitude())) {
            String recvCoordinate = this.amapService.coordinate(hyShippingNote.getReceiveAddress());
            vehicleInfo.setDriverUnloadLonlat(recvCoordinate);
        } else {
            vehicleInfo.setDriverUnloadLonlat(
                    hyShippingNote.getReceiveLongitude().toString()+","+hyShippingNote.getReceiveLatitude().toString()
            );
        }

        //司机信息
        getXzDrivers(hyDriver,shippingNoteDTO);
        //货物信息
        getXzGoodsInfos(hyShippingNote,shippingNoteDTO);
        shippingNoteDTO.setVehicleInfoQuery(vehicleInfo);
    }

    /**
     * 货物信息
     * @param hyShippingNote
     *
     */
    private void getXzGoodsInfos(HyShippingNote hyShippingNote,  XzShippingNoteDTO shippingNoteDTO) {
        List<GoodsInfos> goodsInfos = new ArrayList<>();
        GoodsInfos goodsInfo = new GoodsInfos();
        goodsInfo.setDescriptionOfGoods(hyShippingNote.getDescriptionOfGoods());
        goodsInfo.setCargoTypeClassificationCode(hyShippingNote.getCargoTypeClassificationCode());
        //30吨转换为30000KG后 再乘以1000，中交园区特殊逻辑
        BigDecimal goodsWeightLoading = hyShippingNote.getGoodsWeightLoading().multiply(new BigDecimal("1000000"));
        goodsInfo.setGoodsItemGrossWeight(
                goodsWeightLoading.setScale( 0, BigDecimal.ROUND_DOWN ).intValue()
        );
        //goodsInfo.setCube(10000);
        //        hyOrder.getCube().setScale(4,BigDecimal.ROUND_DOWN)
        //                .toString()
        //goodsInfo.setTotalNumberOfPackages(1);
        goodsInfos.add(goodsInfo);
        shippingNoteDTO.setGoodsInfos(goodsInfos);
    }
    private void getGoodsInfos(HyShippingNote hyShippingNote, VehicleInfo vehicleInfo) {
        List<GoodsInfo> goodsInfos = new ArrayList<>();
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setDescriptionOfGoods(hyShippingNote.getDescriptionOfGoods());
        goodsInfo.setCargoTypeClassificationCode(hyShippingNote.getCargoTypeClassificationCode());
        BigDecimal goodsWeightLoading = hyShippingNote.getGoodsWeightLoading().multiply(new BigDecimal("1000"));
        goodsInfo.setGoodsItemGrossWeight(goodsWeightLoading.setScale(3, BigDecimal.ROUND_DOWN).toString());
        //Modified at 2023-12-11 for 安徽监管将货物毛重字段修改为GoodsItemGrossWeightAct
        goodsInfo.setGoodsItemGrossWeightAct(goodsWeightLoading.setScale(3, BigDecimal.ROUND_DOWN).toString());
        goodsInfo.setCube("1.0000");
        goodsInfo.setTotalNumberOfPackages(1);

        //for test
//        goodsInfo.setGoodsItemGrossWeight("13464.000");
//        goodsInfo.setCube("4.0000");
//        goodsInfo.setTotalNumberOfPackages(2);

        goodsInfos.add(goodsInfo);
        vehicleInfo.setGoodsInfo(goodsInfos);
    }

    private void getZjGoodsInfo(HyShippingNote hyShippingNote, ZjVehicleInfo vehicleInfo) {

        ZjGoodsInfo goodsInfo = new ZjGoodsInfo();
        goodsInfo.setDescriptionOfGoods(hyShippingNote.getDescriptionOfGoods());
        goodsInfo.setCargoTypeClassificationCode(hyShippingNote.getCargoTypeClassificationCode());
        BigDecimal goodsWeightLoading = hyShippingNote.getGoodsWeightLoading().multiply(new BigDecimal("1000"));
        goodsInfo.setGoodsItemGrossWeight(goodsWeightLoading.setScale(3, BigDecimal.ROUND_DOWN).toString());
        //goodsInfo.setCube("1.0000");
        ////        hyOrder.getCube().setScale(4,BigDecimal.ROUND_DOWN)
        ////                .toString()
        //goodsInfo.setTotalNumberOfPackages(1);

        vehicleInfo.setGoodsInfo(
                Collections.singletonList( goodsInfo)
        );
    }



    /**
     * 司机信息
     *
     * @param hyDriver
     * @param vehicleInfo
     */
    private void getDrivers(HyDriver hyDriver, VehicleInfo vehicleInfo) {

        DriverDTO driverDTO = new DriverDTO();
        driverDTO.setDriverName(hyDriver.getDriverName());
        driverDTO.setDrivingLicense(hyDriver.getDrivingLicense());

        vehicleInfo.setDriver(  Collections.singletonList( driverDTO ));
    }
    private void getZjDrivers(HyDriver hyDriver, ZjVehicleInfo vehicleInfo) {

        ZjDriver driverDTO = new ZjDriver();
        driverDTO.setDriverName(hyDriver.getDriverName());
        driverDTO.setDrivingLicense(hyDriver.getDrivingLicense());

        vehicleInfo.setDriver(  Collections.singletonList( driverDTO ));
    }
    private void getXzDrivers(HyDriver hyDriver,  XzShippingNoteDTO shippingNoteDTO) {

        Drivers driverDTO = new Drivers();
        driverDTO.setDriverName(hyDriver.getDriverName());
        driverDTO.setDrivingLicense(hyDriver.getDrivingLicense());

        shippingNoteDTO.setDrivers(Collections.singletonList( driverDTO));
    }

    /**
     * 收货方信息
     *
     */
    public void getConsigneeInfo(HyCompanyConfig companyConfig, HyShippingNote hyShippingNote, ShippingNoteDTO shippingNoteDTO) {
        ConsigneeInfo consigneeInfo = new ConsigneeInfo();
        consigneeInfo.setConsignee(hyShippingNote.getReceiveContactName());
        if (!companyConfig.getReportPlatformType().equals(ReportPlatformEnum.REPORT_PLATFORM_SHAANXI.getCode())) {
            consigneeInfo.setConsigneeId("");
        }
        //for test
        //consigneeInfo.setConsigneeId("119185650762054961");
        //consigneeInfo.setConsigneeID("119185650762054961");

        consigneeInfo.setGoodsReceiptPlace(hyShippingNote.getReceiveAddress());
        consigneeInfo.setCountrySubdivisionCode(hyShippingNote.getReceiveAreaCode());
        shippingNoteDTO.setConsigneeInfo(consigneeInfo);
    }

    private void getZjConsigneeInfo(HyShippingNote hyShippingNote, WaybillDTO shippingNoteDTO) {
        ZjConsigneeInfo consigneeInfo = new ZjConsigneeInfo();

        consigneeInfo.setConsignee(hyShippingNote.getReceiveContactName());
        consigneeInfo.setGoodsReceiptPlace(hyShippingNote.getReceiveAddress());
        consigneeInfo.setCountrySubdivisionCode(hyShippingNote.getReceiveAreaCode());

        shippingNoteDTO.setConsigneeInfo(
                Collections.singletonList( consigneeInfo) );
    }

    private void getXzConsigneeInfo(HyShippingNote hyShippingNote, XzShippingNoteDTO shippingNoteDTO) {
        ConsigneeInfoQuery consigneeInfo = new ConsigneeInfoQuery();
        consigneeInfo.setConsignee(hyShippingNote.getReceiveContactName());
        //consigneeInfo.setConsigneeId(hyShippingNote.getReceiveContactTel());
        consigneeInfo.setGoodsReceiptPlace(hyShippingNote.getReceiveAddress());
        consigneeInfo.setCountrySubdivisionCode(hyShippingNote.getReceiveAreaCode());

        BigDecimal costPayTotal = hyShippingNote.getDriverFee();
        //司机运费
        BigDecimal costPayTotals = costPayTotal.multiply(new BigDecimal("1000"));
        consigneeInfo.setTotalMonetaryAmount(
                costPayTotals.setScale( 0, BigDecimal.ROUND_DOWN ).intValue()
        );
        shippingNoteDTO.setConsigneeInfoQuery(consigneeInfo);
    }




    /**
     * 托运人信息
     */
    public void getConsignorInfo(HyCompanyConfig companyConfig, HyCustomer hyCustomer, HyShippingNote hyShippingNote, ShippingNoteDTO shippingNoteDTO) {
        ConsignorInfo consignorInfo = new ConsignorInfo();
        consignorInfo.setConsignor(hyCustomer.getCustomerName());
        if (!companyConfig.getReportPlatformType().equals(ReportPlatformEnum.REPORT_PLATFORM_SHAANXI.getCode())) {
            consignorInfo.setConsignorId(
                    StringUtils.isNotBlank(hyCustomer.getCreditCode()) ? hyCustomer.getCreditCode() : hyCustomer.getIdcard()
            );
        }
        consignorInfo.setConsignorID(
                StringUtils.isNotBlank(hyCustomer.getCreditCode()) ? hyCustomer.getCreditCode() : hyCustomer.getIdcard()
        );
        consignorInfo.setPlaceOfLoading(hyShippingNote.getSendAddress());
        consignorInfo.setCountrySubdivisionCode(hyShippingNote.getSendAreaCode());
        shippingNoteDTO.setConsignorInfo(consignorInfo);
    }

    public void getXzConsignorInfo(HyCustomer hyCustomer, HyShippingNote hyShippingNote,
                                    XzShippingNoteDTO shippingNoteDTO) {
        ConsignorInfoQuery consignorInfo = new ConsignorInfoQuery();
        consignorInfo.setConsignor(hyCustomer.getCustomerName());
        consignorInfo.setConsignorId( StringUtils.isNotBlank(hyCustomer.getCreditCode()) ? hyCustomer.getCreditCode() : hyCustomer.getIdcard());
        consignorInfo.setPlaceOfLoading(hyShippingNote.getSendAddress());
        consignorInfo.setCountrySubdivisionCode(hyShippingNote.getSendAreaCode());
        shippingNoteDTO.setConsignorInfoQuery(consignorInfo);
    }

    private void getZjConsignorInfo(HyCustomer hyCustomer, HyShippingNote hyShippingNote,
                                    WaybillDTO shippingNoteDTO) {
        ZjConsignorInfo consignorInfo = new ZjConsignorInfo();

        consignorInfo.setConsignor(hyCustomer.getCustomerName());
        consignorInfo.setConsignorID( StringUtils.isNotBlank(hyCustomer.getCreditCode()) ? hyCustomer.getCreditCode() : hyCustomer.getIdcard());
        consignorInfo.setPlaceOfLoading(hyShippingNote.getSendAddress());
        consignorInfo.setCountrySubdivisionCode(hyShippingNote.getSendAreaCode());

        shippingNoteDTO.setConsignorInfo(
                Collections.singletonList(  consignorInfo)
        );
    }

    /**
     * 运费流水单信息
     * @param shippingNoteBills
     * @param hyShippingNote
     * @return
     */
    public FinancialDTO getFinancialDTO(List<HyShippingNoteBill> shippingNoteBills, HyShippingNote hyShippingNote) {
        FinancialDTO financialDTO = new FinancialDTO();
        HyShippingNoteBill shippingNoteBillDb = shippingNoteBills.stream().filter(s->s.getBillType().equals("1")).findAny().orElse(null); //取尾款流水的基础数据
        financialDTO.setDocumentNumber(shippingNoteBillDb.getBillNum());
        financialDTO.setSendToProDateTime(
                DateUtils.dateToStringDateTime(new Date())
        );

        HyVehicle hyVehicle = this.vehicleService.getById(hyShippingNote.getVehicleId());
        if (null == hyVehicle) {
            throw new JeecgBootException("承运车辆查找失败");
        }

        if (hyVehicle.getOwner().contains("公司")) {
            financialDTO.setCarrier(hyVehicle.getOwner() + "(" + shippingNoteBillDb.getCarrier() + ")");
        } else {
            financialDTO.setCarrier(shippingNoteBillDb.getCarrier());
        }

        //陕西监管不能多传参数，会报错
        if (!hyShippingNote.getReportPlatformType().equals(ReportPlatformEnum.REPORT_PLATFORM_SHAANXI.getCode())) {
            financialDTO.setActualCarrierId(shippingNoteBillDb.getActualCarrierId());
        }

        financialDTO.setActualCarrierID(shippingNoteBillDb.getActualCarrierId());

        //For test
//        financialDTO.setCarrier("小白");
//        financialDTO.setActualCarrierId("115396531346589079");
//        financialDTO.setActualCarrierID("115396531346589079");

        financialDTO.setVehicleNumber(shippingNoteBillDb.getVehicleNumber());
        financialDTO.setVehiclePlateColorCode(shippingNoteBillDb.getVehicleColorCode());

        //运单列表
        List <ShippingNoteListDTO> shippingNoteListDTOS = new ArrayList <>();
        ShippingNoteListDTO shippingNoteListDTO = new ShippingNoteListDTO();
        String shippingNoteNumber = shippingNoteBillDb.getShippingNoteNumber().replaceAll("-","");
        if(shippingNoteNumber.length() > 20 ){
            shippingNoteNumber =  shippingNoteNumber.substring(0, 20);
        }
        shippingNoteListDTO.setShippingNoteNumber(shippingNoteNumber);
        shippingNoteListDTO.setSerialNumber("0000");

        BigDecimal costPayShipping = hyShippingNote.getDriverFee() ;

        shippingNoteListDTO.setTotalMonetaryAmount(
                costPayShipping.setScale(3, BigDecimal.ROUND_DOWN).toString()
        );

        shippingNoteListDTOS.add(shippingNoteListDTO);
        financialDTO.setShippingNoteList(shippingNoteListDTOS);
        //财务列表
        List<FinanciallistDTO>  financiallistDTOS = new ArrayList <>();

        for(HyShippingNoteBill shippingNoteBill: shippingNoteBills){

            if (StrUtil.isEmpty(shippingNoteBill.getBankCode()) || StrUtil.isEmpty(CheckBankCardIUtil.getBankNameByBankCode(shippingNoteBill.getBankCode()))) {
                throw new JeecgBootException("资金流水单银行代码错误");
            }

            FinanciallistDTO financiallistDTO = new FinanciallistDTO();
            financiallistDTO.setPaymentMeansCode(
                    PaymentMeansCodeEnum.PAYMENT_MEANS_TYPE_YHZZ.code
            );
            financiallistDTO.setReceiptAccount(
                    "1".equals(shippingNoteBill.getBillImportFlag()) ?
                            shippingNoteBill.getCraccno() :
                            shippingNoteBill.getCraccno()+shippingNoteBill.getCrlogaccno()
            );
            financiallistDTO.setRecipient(shippingNoteBill.getCarrier());
            financiallistDTO.setBankCode(shippingNoteBill.getBankCode());
            financiallistDTO.setSequenceCode(shippingNoteBill.getBillNum());
            financiallistDTO.setMonetaryAmount(
                    shippingNoteBill.getAmt().setScale(3, BigDecimal.ROUND_DOWN).toString()
            );
            financiallistDTO.setDateTime(
                    DateUtils.dateToStringDateTime(shippingNoteBill.getPayDt())
            );

            financiallistDTOS.add(financiallistDTO);
        }

        financialDTO.setFinanciallist(financiallistDTOS);
        return financialDTO;
    }

    /**
     * 司机信息转换
     * @param hyDriver
     * @return
     */

    public DriverDTO hyDriverTODriverDTO(HyDriver hyDriver) {
        if (hyDriver == null) {
            return null;
        }
        DriverDTO driverDTO = new DriverDTO();
        driverDTO.setDriverName(hyDriver.getDriverName());
        driverDTO.setDrivingLicense(hyDriver.getDrivingLicense());
        driverDTO.setVehicleClass(hyDriver.getVehicleClass());
        driverDTO.setQualificationCertificate(
                StringUtils.isNotEmpty(hyDriver.getQualificationCertificate()) ? hyDriver.getQualificationCertificate() : hyDriver.getDrivingLicense()
        );
        driverDTO.setIssuingOrganizations(hyDriver.getIssuingOrganizations());
        driverDTO.setValidPeriodFrom(
                DateUtils.dateToStringNoBar(hyDriver.getValidPeriodFrom())
        );
        driverDTO.setValidPeriodTo(
                DateUtils.dateToStringNoBar(hyDriver.getValidPeriodTo())
        );
        driverDTO.setTelephone(hyDriver.getTelephone());
        return driverDTO;
    }

    /**
     * 车辆信息转换
     * @param hyVehicle
     * @return
     */
    public VehicleDTO hyVehicleDTOTOVehicleDTO(HyVehicle hyVehicle) {
        if (hyVehicle == null) {
            return null;
        }
        VehicleDTO vehicleDTO = new VehicleDTO();
        vehicleDTO.setVehicleNumber(hyVehicle.getVehicleNumber());
        vehicleDTO.setVehiclePlateColorCode(hyVehicle.getVehiclePlateColorCode());
        //重型XX车 转成代码 Q11
        vehicleDTO.setVehicleType(hyVehicle.getVehicleType());
        vehicleDTO.setOwner(hyVehicle.getOwner());
        vehicleDTO.setUseCharacter(hyVehicle.getUseCharacter());
        vehicleDTO.setVin(hyVehicle.getVin());
        vehicleDTO.setIssuingOrganizations(hyVehicle.getIssuingOrganizations());
        vehicleDTO.setRegisterDate(DateUtils.dateToStringNoBar(hyVehicle.getRegisterDate()));
        vehicleDTO.setIssueDate(DateUtils.dateToStringNoBar(hyVehicle.getIssueDate()));
        vehicleDTO.setVehicleEnergyType(hyVehicle.getVehicleEnergyType());
        vehicleDTO.setVehicleTonnage(
                hyVehicle.getVehicleTonnage() != null ?  hyVehicle.getVehicleTonnage()
                        .setScale(2, BigDecimal.ROUND_DOWN).toString() :"40.00"
        );
        vehicleDTO.setGrossMass(
                hyVehicle.getGrossMass() != null ?  hyVehicle.getGrossMass()
                        .setScale(2,BigDecimal.ROUND_DOWN).toString() : "48.00"
        );
        vehicleDTO.setRoadTransportCertificateNumber(
                hyVehicle.getRoadTransportNumber()
        );

        return vehicleDTO;
    }


    /**
     * 挂车车辆信息转换
     * @param hyVehicle
     * @return
     */
    public VehicleDTO hyVehicleToTrailerVehicleDTO(HyVehicle hyVehicle) {
        if (hyVehicle == null) {
            return null;
        }
        VehicleDTO vehicleDTO = new VehicleDTO();
        vehicleDTO.setVehicleNumber(hyVehicle.getTrailerVehiclePlateNumber());
        vehicleDTO.setVehiclePlateColorCode(StrUtil.isEmpty(hyVehicle.getTrailerVehiclePlateColorCode()) ?
                "2" : hyVehicle.getTrailerVehiclePlateColorCode());

        //如果挂车道路运输证号为空可以先用车头的,监管暂时不报错
        if (StrUtil.isEmpty(hyVehicle.getTrailerVehicleRoadTransportNumber())) {
            hyVehicle.setTrailerVehicleRoadTransportNumber(hyVehicle.getRoadTransportNumber());
        }

        List<HyVehicleType> hyVehicleTypeList = vehicleService.getHyVehicleByPid("2");
        HyVehicleType trailerVehicleType = hyVehicleTypeList.stream().filter(s->hyVehicle.getTrailerVehicleType().contains(s.getText())).findAny().orElse(null);
        if (trailerVehicleType == null) {
            vehicleDTO.setVehicleType("B11");
        } else {
            vehicleDTO.setVehicleType(trailerVehicleType.getValue());
        }

        //挂车为无动力，默认Y类型
        vehicleDTO.setVehicleEnergyType("Y");
        vehicleDTO.setVehicleTonnage(
                hyVehicle.getVehicleTonnage() != null ?  hyVehicle.getVehicleTonnage()
                        .setScale(2, BigDecimal.ROUND_DOWN).toString() :"40.00"
        );
        vehicleDTO.setGrossMass(
                hyVehicle.getGrossMass() != null ?  hyVehicle.getGrossMass()
                        .setScale(2,BigDecimal.ROUND_DOWN).toString() : "48.00"
        );
        vehicleDTO.setRoadTransportCertificateNumber(
                hyVehicle.getTrailerVehicleRoadTransportNumber()
        );

        return vehicleDTO;
    }


    /**
     * 上报运单查询运单轨迹是否存在
     * @param hyShippingNote
     */
    public void checkShippingNoteRoute(HyShippingNote hyShippingNote) {
        String startTime = DateUtils.dateTimeToString(hyShippingNote.getStatusDtLoading());
        String endTime = DateUtils.dateTimeToString(hyShippingNote.getStatusDtUnloading());
        HyVehicle hyVehicle = vehicleService.getById(hyShippingNote.getVehicleId());
        if(hyVehicle.getVehicleType().contains("重型")){
            String vehicleColor = StringUtils.isNotEmpty(hyVehicle.getVehiclePlateColorCode()) ? hyVehicle.getVehiclePlateColorCode() : "2";
            WaybillRouteVO waybillRouteVO = waybillRouteService.getWaybillRoute(hyVehicle.getVehicleNumber(), vehicleColor, startTime, endTime, "GCJ02");
            if (waybillRouteVO == null || CollectionUtils.isEmpty(waybillRouteVO.getPointDataList())) {
                throw new JeecgBootException("该运单没有运单轨迹！");
            }
        }

    }

    /**
     * 运单信息 中交
     * @param hyShippingNote
     * @param companyId
     * @return
     */
    public WaybillDTO shippingNoteTOWaybillDTO(HyShippingNote hyShippingNote, String companyId) {
        if (hyShippingNote == null) {
            throw new JeecgBootException("该运单上报信息不能为空");
        }

        HyCustomer hyCustomer = customerService.getById(hyShippingNote.getCustomerId());
        if (hyCustomer == null) {
            throw new JeecgBootException("该运单货主信息不能为空");
        }

        HyOrder hyOrder = orderService.getById(hyShippingNote.getOrderId());
        if (hyOrder == null) {
            throw new JeecgBootException("该运单货源信息不能为空");
        }

        HyDriver hyDriver = driverService.getById(hyShippingNote.getDriverId());

        if (hyDriver == null) {
            throw new JeecgBootException("该运单司机信息不能为空");
        }
        HyVehicle hyVehicle = vehicleService.getById(hyShippingNote.getVehicleId());
        if (hyVehicle == null) {
            throw new JeecgBootException("该运单车辆信息不能为空");
        }
        //申报公司：河北一六八网络科技有限公司

        HyCompany company = companyService.getById(companyId);
        if (company == null) {
            throw new JeecgBootException("该运单公司信息不能为空");
        }

        WaybillDTO shippingNoteDTO = new WaybillDTO();
        shippingNoteDTO.setSerialNo(hyShippingNote.getId()+"_"+  RandomUtil.randomNumbers(4));
        shippingNoteDTO.setOriginalDocumentNumber(hyOrder.getOrderNum());
        String shippingNoteNumber = hyShippingNote.getShippingNoteNumber().replaceAll("-", "");
        if (shippingNoteNumber.length() > 20) {
            shippingNoteNumber = shippingNoteNumber.substring(0, 20);
        }
        shippingNoteDTO.setShippingNoteNumber(shippingNoteNumber);

        //Integer vehicleAmounthy = getVehicleAmounthyByOrderId(hyShippingNote.getOrderId()) ;
        shippingNoteDTO.setVehicleAmount("1");
        //分段分单号 分段运输和多车运输由 四位数字组成， 前两位代表一 单多车的序号，后两位代表分 段序号。
        //若运输形式为一单一 车填 0000。
        shippingNoteDTO.setSerialNumber("0000");
        //运输组织类型代码
        shippingNoteDTO.setTransportTypeCode("1");
        shippingNoteDTO.setSendToProDateTime(DateUtils.dateToStringDateTime(new Date()));

        shippingNoteDTO.setCarrier(company.getCompanyName());
        shippingNoteDTO.setUnifiedSocialCreditIdentifier(company.getCreditCode() );
        //车辆 道路运输经营许可 网络货运经营者的道路 运输经营许可证编号。
        shippingNoteDTO.setPermitNumber(company.getPermitNumber() );

        shippingNoteDTO.setConsignmentDateTime(
                DateUtils.dateToStringDateTime(hyShippingNote.getCreateTime())
        );
        shippingNoteDTO.setBusinessTypeCode(hyShippingNote.getBusinessTypeCode());

        shippingNoteDTO.setDespatchActualDateTime(
                DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtLoading())
        );
        shippingNoteDTO.setGoodsReceiptDateTime(
                DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtUnloading())
        );
        BigDecimal costPayShipping = hyShippingNote.getDriverFee() ;

        shippingNoteDTO.setTotalMonetaryAmount(
                costPayShipping.setScale(3, BigDecimal.ROUND_DOWN).toString()
        );
        //托运人信息
        getZjConsignorInfo(hyCustomer, hyShippingNote, shippingNoteDTO);
        //收货方信息
        getZjConsigneeInfo(hyShippingNote, shippingNoteDTO);
        //车辆信息
        getZjVehicleInfo(hyShippingNote, hyDriver, hyVehicle, shippingNoteDTO);
        //实际承运人信息
        getZjActualCarrierInfo(hyVehicle, hyDriver, shippingNoteDTO);
        //保险信息
        getZjInsuranceInformation(shippingNoteDTO);

        return shippingNoteDTO;
    }

    /**
     * 运费流水 中交
     * @param shippingNoteBills
     * @param hyShippingNote
     * @return
     */
    public   CapitalDTO getCapitalDTO(List<HyShippingNoteBill> shippingNoteBills, HyShippingNote hyShippingNote){
        CapitalDTO financialDTO = new CapitalDTO();
        HyShippingNoteBill shippingNoteBillDb = shippingNoteBills.get(0);

        financialDTO.setSerialNo(shippingNoteBillDb.getId()+"_"+  RandomUtil.randomNumbers(4));
        financialDTO.setDocumentNumber(shippingNoteBillDb.getBillNum());
        financialDTO.setSendToProDateTime(
                DateUtils.dateToStringDateTime(new Date())
        );

        HyVehicle hyVehicle = this.vehicleService.getById(hyShippingNote.getVehicleId());
        if (null == hyVehicle) {
            throw new JeecgBootException("承运车辆查找失败");
        }
        if (hyVehicle.getOwner().contains("公司")) {
            financialDTO.setCarrier(hyVehicle.getOwner() + "(" + shippingNoteBillDb.getCarrier() + ")");
        } else {
            financialDTO.setCarrier(shippingNoteBillDb.getCarrier());
        }

        financialDTO.setActualCarrierID(shippingNoteBillDb.getActualCarrierId());
        financialDTO.setVehicleNumber(shippingNoteBillDb.getVehicleNumber());
        financialDTO.setVehiclePlateColorCode(shippingNoteBillDb.getVehicleColorCode());

        //运单列表
        List <ZjShippingNoteList> shippingNoteListDTOS = new ArrayList <>();
        ZjShippingNoteList shippingNoteListDTO = new ZjShippingNoteList();
        String shippingNoteNumber = shippingNoteBillDb.getShippingNoteNumber().replaceAll("-","");
        if(shippingNoteNumber.length() > 20 ){
            shippingNoteNumber =  shippingNoteNumber.substring(0, 20);
        }
        shippingNoteListDTO.setShippingNoteNumber(shippingNoteNumber);
        shippingNoteListDTO.setSerialNumber("0000");
        BigDecimal costPayShipping = hyShippingNote.getDriverFee() ;


        shippingNoteListDTO.setTotalMonetaryAmount(
                costPayShipping.setScale(3, BigDecimal.ROUND_DOWN).toString()
        );

        shippingNoteListDTOS.add(shippingNoteListDTO);
        financialDTO.setShippingNoteList(shippingNoteListDTOS);
        //财务列表
        List<ZjFinanciallist>  financiallistDTOS = new ArrayList <>();
        for(HyShippingNoteBill shippingNoteBill:shippingNoteBills ){
            ZjFinanciallist financiallistDTO = new ZjFinanciallist();

            financiallistDTO.setPaymentMeansCode(
                    PaymentMeansCodeEnum.PAYMENT_MEANS_TYPE_YHZZ.code
            );
            financiallistDTO.setReceiptAccount(
                    "1".equals(shippingNoteBill.getBillImportFlag()) ?
                            shippingNoteBill.getCraccno() :
                            shippingNoteBill.getCraccno()+shippingNoteBill.getCrlogaccno()
            );
            financiallistDTO.setRecipient(shippingNoteBill.getCarrier());
            //financiallistDTO.setBankCode(shippingNoteBill.getBankCode());
            financiallistDTO.setSequenceCode(shippingNoteBill.getBillNum());
            financiallistDTO.setMonetaryAmount(
                    shippingNoteBill.getAmt().setScale(3, BigDecimal.ROUND_DOWN).toString()

            );
            financiallistDTO.setDateTime(
                    DateUtils.dateToStringDateTime(shippingNoteBill.getPayDt())
            );
            financiallistDTOS.add(financiallistDTO);
        }

        financialDTO.setFinanciallist(financiallistDTOS);
        return financialDTO;
    }

    /**
     *  产业园 运单数据
     * @param hyShippingNote
     * @param companyId
     * @return
     */
    public XzShippingNoteDTO shippingNoteTOXzShippingNoteDTO(HyShippingNote hyShippingNote, String companyId) {
        if (hyShippingNote == null) {
            throw new JeecgBootException("该运单上报信息不能为空");
        }

        HyCustomer hyCustomer = customerService.getById(hyShippingNote.getCustomerId());
        if (hyCustomer == null) {
            throw new JeecgBootException("该运单货主信息不能为空");
        }

        HyCustomerCompany hyCustomerCompany = this.hyCustomerCompanyService.getByCustomerId(hyCustomer.getId());
        if (null == hyCustomerCompany) {
            throw new JeecgBootException("该运单货主签约信息不能为空");
        }
        if (StrUtil.isEmpty(hyCustomerCompany.getImgContract())) {
            throw new JeecgBootException("该运单货主签约合同不能为空，请上传货主合同");
        }

        HyOrder hyOrder = orderService.getById(hyShippingNote.getOrderId());
        if (hyOrder == null) {
            throw new JeecgBootException("该运单货源信息不能为空");
        }

        HyDriver hyDriver = driverService.getById(hyShippingNote.getDriverId());

        if (hyDriver == null) {
            throw new JeecgBootException("该运单司机信息不能为空");
        }
        HyVehicle hyVehicle = vehicleService.getById(hyShippingNote.getVehicleId());
        if (hyVehicle == null) {
            throw new JeecgBootException("该运单车辆信息不能为空");
        }
        HyCompany company = companyService.getById(companyId);
        if (company == null) {
            throw new JeecgBootException("该运单公司信息不能为空");
        }

        XzShippingNoteDTO shippingNoteDTO = new XzShippingNoteDTO();

        shippingNoteDTO.setOriginalDocumentNumber(hyOrder.getOrderNum());
        String shippingNoteNumber = hyShippingNote.getShippingNoteNumber().replaceAll("-", "");
        if (shippingNoteNumber.length() > 20) {
            shippingNoteNumber = shippingNoteNumber.substring(0, 20);
        }
        shippingNoteDTO.setShippingNoteNumber(shippingNoteNumber);

        shippingNoteDTO.setVehicleAmount(1);
        //分段分单号 分段运输和多车运输由 四位数字组成， 前两位代表一 单多车的序号，后两位代表分 段序号。
        //若运输形式为一单一 车填 0000。
        shippingNoteDTO.setSerialNumber("0000");
        //运输组织类型代码
        shippingNoteDTO.setTransportTypeCode(1);
        shippingNoteDTO.setSendToProDateTime(
                DateUtils.dateTimeToString(new Date())
        );

        shippingNoteDTO.setCarrier(company.getCompanyName() );
        shippingNoteDTO.setUnifiedSocialCreditIdentifier(company.getCreditCode() );
        //车辆 道路运输经营许可 网络货运经营者的道路 运输经营许可证编号。
        shippingNoteDTO.setPermitNumber(company.getPermitNumber() );

        shippingNoteDTO.setConsignmentDateTime(
                DateUtils.dateTimeToString(hyShippingNote.getCreateTime())
        );
        shippingNoteDTO.setBusinessTypeCode(
                Integer.parseInt(hyShippingNote.getBusinessTypeCode())
        );

        shippingNoteDTO.setDespatchActualDateTime(
                DateUtils.dateTimeToString(hyShippingNote.getStatusDtLoading())
        );
        shippingNoteDTO.setGoodsReceiptDateTime(
                DateUtils.dateTimeToString(hyShippingNote.getStatusDtUnloading())
        );

        //货主合同必填
        String customerContract = xzIndustrialParkService.uploadFile(  uploadpath + File.separator  + hyCustomerCompany.getImgContract(), "货主合同文件");
        shippingNoteDTO.setOwnersContractFile(Collections.singletonList(customerContract));

        //合同必填
        String shippingContract = xzIndustrialParkService.uploadFile(  uploadpath + File.separator  + hyShippingNote.getShippingContract(), "运单合同文件");
        shippingNoteDTO.setFile(Collections.singletonList(shippingContract));

        //装卸货照片非必填
//        if (StrUtil.isNotEmpty(hyShippingNote.getLoadingSceneImg())) {
//            String loadingSceneImg = xzIndustrialParkService.uploadFile(  uploadpath + File.separator  + hyShippingNote.getLoadingSceneImg().split(",")[0], "运单装货现场照片");
//            shippingNoteDTO.setUnLoadGoodsImages(Collections.singletonList(loadingSceneImg));
//        }
//
//        if (StrUtil.isNotEmpty(hyShippingNote.getUnloadingSceneImg())) {
//            String unloadingSceneImg = xzIndustrialParkService.uploadFile(  uploadpath + File.separator  + hyShippingNote.getUnloadingSceneImg().split(",")[0], "运单卸货现场照片");
//            shippingNoteDTO.setLoadGoodsImages(Collections.singletonList(unloadingSceneImg));
//        }

        //磅单照片必填
        List<String> backDocumentImages = new ArrayList<>() ;
        if (StrUtil.isNotEmpty(hyShippingNote.getImgLoading())) {
            for (int i = 0; i < hyShippingNote.getImgLoading().split(",").length; i++) {
                String imgLoading = xzIndustrialParkService.uploadFile(  uploadpath + File.separator  + hyShippingNote.getImgLoading().split(",")[i], "(回单)装货凭证照片");
                backDocumentImages.add(imgLoading);
            }
        }

        if (StrUtil.isNotEmpty(hyShippingNote.getImgUnloading())) {
            for (int i = 0; i < hyShippingNote.getImgUnloading().split(",").length; i++) {
                String imgUnLoading = xzIndustrialParkService.uploadFile(  uploadpath + File.separator  + hyShippingNote.getImgUnloading().split(",")[i], "(回单)卸货凭证照片");
                backDocumentImages.add(imgUnLoading);
            }
        }

        shippingNoteDTO.setBackDocumentImages(backDocumentImages );

        //托运人信息
        getXzConsignorInfo(hyCustomer, hyShippingNote, shippingNoteDTO);
        //收货方信息
        getXzConsigneeInfo(hyShippingNote, shippingNoteDTO);
        //车辆信息
        getXzVehicleInfo(hyShippingNote, hyDriver, hyVehicle, shippingNoteDTO);
        //实际承运人信息
        getXzActualCarrierInfo(hyVehicle, hyDriver, shippingNoteDTO);
        //保险信息
        getXzInsuranceInformation(shippingNoteDTO);

        return shippingNoteDTO;
    }

    /**
     * 产业园 运费流水数据
     * @param shippingNoteBills
     * @param hyShippingNote
     * @return
     */
    public XzPaymentDTO getXzFinancialDTO(List<HyShippingNoteBill> shippingNoteBills,
                                                  HyShippingNote hyShippingNote ) {
        XzPaymentDTO financialDTO = new XzPaymentDTO();
        HyShippingNoteBill shippingNoteBillDb = shippingNoteBills.get(0);

        financialDTO.setDocumentNumber(shippingNoteBillDb.getBillNum());
        financialDTO.setSendToProDateTime(
                DateUtils.dateTimeToString(new Date())
        );
        financialDTO.setCarrier(shippingNoteBillDb.getCarrier());
        financialDTO.setActualCarrierId(shippingNoteBillDb.getActualCarrierId());

        financialDTO.setVehicleNumber(shippingNoteBillDb.getVehicleNumber());
        financialDTO.setVehiclePlateColorCode(
                Integer.parseInt(shippingNoteBillDb.getVehicleColorCode())
        );

        //运单列表
        List <XzShippingNoteListDTO> shippingNoteListDTOS = new ArrayList <>();
        XzShippingNoteListDTO shippingNoteListDTO = new XzShippingNoteListDTO();
        String shippingNoteNumber = shippingNoteBillDb.getShippingNoteNumber().replaceAll("-","");
        if(shippingNoteNumber.length() > 20 ){
            shippingNoteNumber =  shippingNoteNumber.substring(0, 20);
        }
        shippingNoteListDTO.setShippingNoteNumber(shippingNoteNumber);
        shippingNoteListDTO.setSerialNumber("0000");


        BigDecimal costPayShipping = hyShippingNote.getDriverFee() ;

        HyDriver driver = driverService.getById(hyShippingNote.getDriverId());

        BigDecimal totalMonetaryAmount = costPayShipping.multiply(new BigDecimal("1000"));

        shippingNoteListDTO.setTotalMonetaryAmount(
                totalMonetaryAmount.setScale( 0, BigDecimal.ROUND_DOWN ).intValue()
        );
        shippingNoteListDTO.setFuelAmount(0);

        shippingNoteListDTOS.add(shippingNoteListDTO);
        financialDTO.setShippingNoteList(shippingNoteListDTOS);
        //财务列表
        List<XzFinancialListDTO>  financiallistDTOS = new ArrayList <>();

        for(HyShippingNoteBill shippingNoteBill : shippingNoteBills){
            XzFinancialListDTO financiallistDTO = new XzFinancialListDTO();

            financiallistDTO.setShippingNoteNumber(shippingNoteNumber);
            financiallistDTO.setSerialNumber("0000");
            financiallistDTO.setDriverName(shippingNoteBill.getCarrier());
            financiallistDTO.setDriverLicense(shippingNoteBill.getActualCarrierId());
            financiallistDTO.setPaymentMeansCode(
                    PaymentMeansCodeEnum.PAYMENT_MEANS_TYPE_YHZZ.code
            );
            financiallistDTO.setReceiptAccount(
                    "1".equals(shippingNoteBill.getBillImportFlag()) ?
                            shippingNoteBill.getCraccno() :
                            shippingNoteBill.getCraccno()+shippingNoteBill.getCrlogaccno()
            );
            financiallistDTO.setRecipient(shippingNoteBill.getCarrier());
            financiallistDTO.setBankCode(shippingNoteBill.getBankCode());
            financiallistDTO.setSequenceCode(shippingNoteBill.getBillNum());

            BigDecimal monetaryAmount = shippingNoteBill.getAmt().multiply(new BigDecimal("1000"));
            financiallistDTO.setMonetaryAmount(
                    monetaryAmount.setScale( 0, BigDecimal.ROUND_DOWN ).intValue()
            );
            financiallistDTO.setDateTime(
                    DateUtils.dateTimeToString(shippingNoteBill.getPayDt())
            );
            financiallistDTO.setPayFile(shippingNoteBill.getBankHd());

            //园区新增字段2023年9月25日16:13:50
            //如果配置了黑龙江园区，这里需要增加一些字段
            String heilongjiangPark = HyCfgUtil.getCfgItem2("yuanqu_report","heilongjiang_park");
            if (StrUtil.isNotEmpty(heilongjiangPark)) {
                financiallistDTO.setPhone(driver.getTelephone());
                financiallistDTO.setIdcardNumber(driver.getIdcard());
                financiallistDTO.setSex(driver.getSex());

                String birthDay = "";
                try {
                    Date format = new SimpleDateFormat("yyyyMMdd").parse(driver.getIdcard().substring(6, 14));
                    birthDay = new SimpleDateFormat("yyyy-MM-dd").format(format);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("身份证日期转换异常", e.getMessage());
                }

                log.info("生日转换 {} -> {}", driver.getIdcard(), birthDay);
                if (StrUtil.isNotEmpty(birthDay)) {
                    financiallistDTO.setBirthday(birthDay);
                } else {
                    financiallistDTO.setBirthday(DateUtil.format(driver.getBirthday(), "yyyy-MM-dd"));
                }

                financiallistDTO.setAddress(driver.getAddress());

                if (StrUtil.isNotEmpty(driver.getIdcardImg())) {
                    String idcardImg = xzIndustrialParkService.uploadFile(  uploadpath + File.separator  + driver.getIdcardImg(), "身份证正面照片");
                    financiallistDTO.setRecipientIdCardFrontImg(idcardImg);
                }

                if (StrUtil.isNotEmpty(driver.getIdcardImgBack())) {
                    String idcardImgBack = xzIndustrialParkService.uploadFile(  uploadpath + File.separator  + driver.getIdcardImgBack(), "身份证反面照片");
                    financiallistDTO.setRecipientIdCardBackImg(idcardImgBack);
                }
            }

            financiallistDTOS.add(financiallistDTO);
        }

        financialDTO.setFinancialList(financiallistDTOS);


        return financialDTO;
    }

    /**
     * 获取企业信息
     * @param hyEnterprise
     * @param actionType
     * @return
     */
   public EnterpriseDTO  getEnterpriseDTO(HyEnterprise hyEnterprise ,String actionType){
       EnterpriseDTO enterpriseDTO = enterpriseTOEnterpriseDTO(hyEnterprise,actionType);
//       if(enterpriseDTO != null){
//           List <AppsDTO> apps = new ArrayList <>();
//           QueryWrapper<HyEnterpriseApps> queryWrapper = new QueryWrapper();
//           queryWrapper.eq("iz_used",1);
//           List <HyEnterpriseApps> enterpriseApps = enterpriseAppsService.list(queryWrapper);
//           if(CollectionUtils.isNotEmpty(enterpriseApps)){
//               for(HyEnterpriseApps enterpriseApp : enterpriseApps){
//                   AppsDTO appsDTO = new AppsDTO();
//                   appsDTO.setAppId(enterpriseApp.getAppId());
//                   appsDTO.setAppName(enterpriseApp.getAppName());
//                   appsDTO.setAppSecurity(enterpriseApp.getAppSecurity());
//                   appsDTO.setAppType(enterpriseApp.getAppType());
//                   apps.add(appsDTO);
//               }
//           }
//           enterpriseDTO.setApps(apps);
//       }
       return enterpriseDTO ;
    }

    /**
     * 转换企业信息
     * @param hyEnterprise
     * @param actionType
     * @return
     */
    private static EnterpriseDTO enterpriseTOEnterpriseDTO(HyEnterprise hyEnterprise,String actionType) {
        if (hyEnterprise == null) {
            return null;
        }
        EnterpriseDTO enterpriseDTO = new EnterpriseDTO();
        //操作类型  1:新增操作 2：更新操作
        enterpriseDTO.setActionType(actionType);
        enterpriseDTO.setEntName(hyEnterprise.getEntName());
        enterpriseDTO.setUnifiedSocialCreditIdentifier(hyEnterprise.getUnifiedSocialCreditIdentifier());
        enterpriseDTO.setEntAddress(hyEnterprise.getEntAddress());
        enterpriseDTO.setAreaCode(hyEnterprise.getAreaCode());
        enterpriseDTO.setRegisterMoney(hyEnterprise.getRegisterMoney());
        enterpriseDTO.setRegisterDate(
                DateUtils.dateToStringNoBar(hyEnterprise.getRegisterDate())
        );
        enterpriseDTO.setRunScope(hyEnterprise.getRunScope());
        enterpriseDTO.setPermitNumber(hyEnterprise.getPermitNumber());
        enterpriseDTO.setFaxNo(hyEnterprise.getFaxNo());
        enterpriseDTO.setLegalerName(hyEnterprise.getLegalerName());
        enterpriseDTO.setLegalerTel(hyEnterprise.getLegalerTel());
        enterpriseDTO.setContactsName(hyEnterprise.getContactsName());
        enterpriseDTO.setContactsTel(hyEnterprise.getContactsTel());
        //监测状态  监测状态：0：未开启；1：联调测试开启；2：正式监测开启
        enterpriseDTO.setMonitorsState(hyEnterprise.getMonitorsState());
        enterpriseDTO.setMonitorTime(
                DateUtils.dateToStringDateTime(hyEnterprise.getMonitorTime())
        );
        enterpriseDTO.setEnterpriseCreateTime(
                DateUtils.dateToStringNoBar(hyEnterprise.getEnterpriseCreateTime())
        );
        enterpriseDTO.setIpcNum(hyEnterprise.getIpcnum());
        enterpriseDTO.setPsnrnum(hyEnterprise.getPsnrnum());
        return enterpriseDTO;
    }


    //安徽上报运单第二步
    public ShippingNoteDTO shippingNoteTOShippingNoteDTOAhStep2(HyShippingNote hyShippingNote) {
        if (hyShippingNote == null) {
            throw new JeecgBootException("该运单上报信息不能为空");
        }

        HyCustomer hyCustomer = customerService.getById(hyShippingNote.getCustomerId());
        if (hyCustomer == null) {
            throw new JeecgBootException("该运单货主信息不能为空");
        }

        HyOrder hyOrder = orderService.getById(hyShippingNote.getOrderId());
        if (hyOrder == null) {
            throw new JeecgBootException("该运单货源信息不能为空");
        }

        HyVehicle hyVehicle = vehicleService.getById(hyShippingNote.getVehicleId());
        if (hyVehicle == null) {
            throw new JeecgBootException("该运单车辆信息不能为空");
        }

        ShippingNoteDTO shippingNoteDTO = new ShippingNoteDTO();

        shippingNoteDTO.setOriginalDocumentNumber(hyOrder.getOrderNum());
        String shippingNoteNumber = hyShippingNote.getShippingNoteNumber().replaceAll("-", "");
        if (shippingNoteNumber.length() > 20) {
            shippingNoteNumber = shippingNoteNumber.substring(0, 20);
        }
        shippingNoteDTO.setShippingNoteNumber(shippingNoteNumber);

        //Integer vehicleAmounthy = getVehicleAmounthyByOrderId(hyShippingNote.getOrderId()) ;
        shippingNoteDTO.setVehicleAmount(1);
        //分段分单号 分段运输和多车运输由 四位数字组成， 前两位代表一 单多车的序号，后两位代表分 段序号。
        //若运输形式为一单一 车填 0000。
        shippingNoteDTO.setSerialNumber("0000");
        shippingNoteDTO.setSendToProDateTime(DateUtils.dateToStringDateTime(new Date()));
        shippingNoteDTO.setGoodsReceiptDateTime(DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtUnloading()));

        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setVehiclePlateColorCode(hyVehicle.getVehiclePlateColorCode());
        vehicleInfo.setVehicleNumber(hyVehicle.getVehicleNumber());
        vehicleInfo.setGoodsReceiptDateTime(
                DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtUnloading())
        );

        shippingNoteDTO.setVehicleInfo(vehicleInfo);

        return shippingNoteDTO;
    }


    public ShippingNoteDTO shippingNoteTOShippingNoteDTOAhStep3(HyShippingNote hyShippingNote) {
        if (hyShippingNote == null) {
            throw new JeecgBootException("该运单上报信息不能为空");
        }

        HyCustomer hyCustomer = customerService.getById(hyShippingNote.getCustomerId());
        if (hyCustomer == null) {
            throw new JeecgBootException("该运单货主信息不能为空");
        }

        HyOrder hyOrder = orderService.getById(hyShippingNote.getOrderId());
        if (hyOrder == null) {
            throw new JeecgBootException("该运单货源信息不能为空");
        }

        HyVehicle hyVehicle = vehicleService.getById(hyShippingNote.getVehicleId());
        if (hyVehicle == null) {
            throw new JeecgBootException("该运单车辆信息不能为空");
        }

        ShippingNoteDTO shippingNoteDTO = new ShippingNoteDTO();
        shippingNoteDTO.setOriginalDocumentNumber(hyOrder.getOrderNum());
        String shippingNoteNumber = hyShippingNote.getShippingNoteNumber().replaceAll("-", "");
        if (shippingNoteNumber.length() > 20) {
            shippingNoteNumber = shippingNoteNumber.substring(0, 20);
        }
        shippingNoteDTO.setShippingNoteNumber(shippingNoteNumber);

        //Integer vehicleAmounthy = getVehicleAmounthyByOrderId(hyShippingNote.getOrderId()) ;
        shippingNoteDTO.setVehicleAmount(1);
        //分段分单号 分段运输和多车运输由 四位数字组成， 前两位代表一 单多车的序号，后两位代表分 段序号。
        //若运输形式为一单一 车填 0000。
        shippingNoteDTO.setSerialNumber("0000");
        shippingNoteDTO.setSendToProDateTime(DateUtils.dateToStringDateTime(new Date()));
        //shippingNoteDTO.setGoodsReceiptDateTime(DateUtils.dateToStringDateTime(hyShippingNote.getStatusDtUnloading()));
        BigDecimal costPay = hyShippingNote.getGoodsWeightLoading().multiply(hyShippingNote.getShippingUnitPrice());
        BigDecimal costPayShipping = hyShippingNote.getDriverFee() != null ?  hyShippingNote.getDriverFee() : costPay ;

        shippingNoteDTO.setTotalMonetaryAmount(
                costPayShipping.setScale(3, BigDecimal.ROUND_DOWN).toString());

        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setVehiclePlateColorCode(hyVehicle.getVehiclePlateColorCode());
        vehicleInfo.setVehicleNumber(hyVehicle.getVehicleNumber());
        //货物信息
        getGoodsInfos(hyShippingNote, vehicleInfo);

        shippingNoteDTO.setVehicleInfo(vehicleInfo);

        return shippingNoteDTO;
    }


    //根据运单中的addressid获取经纬度，再通过经纬度获取运输距离
    public double getShippingNoteDistance(HyShippingNote hyShippingNote) {
        HyCustomerAddressSend hyCustomerAddressSend = this.customerAddressSendService.getById(hyShippingNote.getAddressSendId());
        HyCustomerAddressReceive hyCustomerAddressReceive = this.customerAddressReceiveService.getById(hyShippingNote.getAddressReceiveId());
        if (null == hyCustomerAddressSend || null == hyCustomerAddressReceive) {
            log.warn("收发货地址地图坐标选取不能为空, {} {}", JSONUtil.toJsonStr(hyCustomerAddressSend), JSONUtil.toJsonStr(hyCustomerAddressReceive));
            return 0d;
        }

        double m = LngLonUtil.GetDistance(hyCustomerAddressSend.getLatitude(), hyCustomerAddressSend.getLongitude(),
                hyCustomerAddressReceive.getLatitude(), hyCustomerAddressReceive.getLongitude());
        double km =  m > 0d ? Math.round((m/100d)/10d) : 0d;
        return km;
    }


    //上报运单和资金流水前，先确认司机、车辆的上传状态
    public Result checkDriverAndVehicle(String shippingId) {

        HyShippingNote hyShippingNote = this.hyShippingNoteService.getById(shippingId);
        if (null == hyShippingNote) {
            return Result.error("运单不存在");
        }

        String driverId = hyShippingNote.getDriverId();
        String vehicleId = hyShippingNote.getVehicleId();
        if (StrUtil.isEmpty(driverId)) {
            return Result.error("当前运单司机信息不存在");
        }

        if (StrUtil.isEmpty(vehicleId)) {
            return Result.error("当前运单车辆信息不存在");
        }

        HyDriver driver = this.driverService.getById(driverId);
        HyVehicle vehicle = this.vehicleService.getById(vehicleId);
        if (!driver.getUploadStatus().equals("1")) {
            return Result.error("请先同步当前司机数据");
        }

        if (!vehicle.getUploadStatus().equals("1")) {
            return Result.error("请先同步当前车辆数据");
        }

        return Result.OK();
    }
}
